package com.tao.schedule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.time.LocalDate;

/**
 * @ClassName SendMail
 * @Author HETAO
 * @Date 2020/4/15 20:41
 */
@Service
public class SendMail {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    //秒 分 时 日 月 星期
    @Scheduled(cron = "0/2 * * * * ?")
    public void test() throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);

        messageHelper.setTo("1162628323@qq.com");
        messageHelper.setSubject("来自遥远的星球");
        messageHelper.setText("<p style='color:red'>" + LocalDate.now().toString() + "</p>", true);
        //messageHelper.addAttachment("1.png",new File("C:\\Users\\Administrator\\Desktop\\1.png"));
        messageHelper.setFrom("study.hetao@foxmail.com");
        System.out.println("发送");
        javaMailSender.send(mimeMessage);
    }
}
